<?php
namespace Home\Controller;
use Think\Controller;
class RecreationController extends Controller {
    public function index(){
        $this->assign('active',0);

        $this->assign('titleName',titleName('Rcat',1));//首页名称

        indexRvent();//测算首页执行事件
        indexRequest($_REQUEST);//测算首页接收数据处理

        if(cookie('jploginHC')==''){
            cnzz_pvuv('MAJ');//执行统计
            cookie('jploginHC',1,30);
        }

        $channel=cookie('channel');
        if($_REQUEST['cs']){
            cookie('price',0.01);//默认价格
        }else{
            cookie('price',9);//默认价格
            channelPay($channel,'MAJ');//查询渠道价格
        }

        //channelPay($channel,'MAJ',1);//查询渠道价格

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $this->assign('wxlogin',1);

            cookie('wxlogin',1);
            getToken();
            getTicket();
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl=wxJsdkData();
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
            if(!empty($_REQUEST['code'])){
                cookie('code',$_REQUEST['code'],7100);
            }
            getCode($wzurl);
            getOopenid();
            cookie('uid',cookie('openid'));
        }


        if($_REQUEST['kind']){
            $kind=$_REQUEST['kind'];
        }else{
            $kind=0;
        }
        $this->assign('kind',$kind);
        $this->display('Recreation/index');
    }
    public function cookieUserData(){//缓存用户信息
       $birthday=$_REQUEST['birthday'];
        $username=$_REQUEST['username'];
        if(empty($username)){
            $username='匿名';
        }
        $gender=$_REQUEST['gender'];
        cookie('birthday',$birthday);
        cookie('username',$username);
        cookie('gender',$gender);
       echo json_encode($birthday.$username.$gender);
    }
    public function cookiePid(){//缓存Pid
        $pid=mt_rand(1,42);
        cookie('pid',$pid);
        $majiang=new \Library\MaJiang();
        $arr=$majiang->index($pid);
        $arr['username']= cookie('username');
        echo json_encode($arr);
    }

    public function zhifu(){
        $birthday=cookie('birthday');
        $username=cookie('username');
        $gender=cookie('gender');

        $birthdayArr=explode('-',$birthday);

        $y=$birthdayArr[1];//年
        $m=$birthdayArr[2]; //月
        $d=$birthdayArr[3];//日
        $zyinli=yinli($y,$m,$d);

        $data=cookie('orderid');//获取订单号
        if(empty($data)){
            $orderid='MAJ'.date('Ymd').mt_rand(10000,99999).date('His');
            cookie('orderid',$orderid,604800);
        }

        //创建订单
        $hour=$birthdayArr[4];
        $shop=cookie('orderid');
        $znickname=$username;
        $zsex=$gender;
        $channel=cookie('channel');
        $imei=cookie('uid');
        $datetype=$birthdayArr[0];if($datetype=='')$datetype=0;
        //defaultPrice();

        //csPay($znickname,$channel);//测试价格判断

        $price=cookie('price');
        if(empty($_REQUEST['ordernum'])){
            $arrdata=S('sm_h5test|'.$shop);
            if(empty($arrdata)){
                $smH5testinter=array(
                    'ordernum'=>$shop, 'price'=>$price, 'username'=>$znickname, 'typeid'=>40, 'sex'=>$zsex, 'createtime'=>date('Y-m-d H:i:m'),
                    'status'=>-1, 'datetype'=>$datetype, 'year'=>$y, 'month'=>$m, 'day'=>$d, 'hour'=>$hour, 'remark'=>cookie('pid'),
                    'appuserid'=>$imei, 'ip'=>$_SERVER['REMOTE_ADDR'], 'channel'=>$channel, 'auto_send_sms'=>0, 'place'=>0,
                );
                S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                cacheZhiFuOrderid($shop);//支付页缓存订单操作

                $IndexController=A('Index');//Index控制器
                $IndexController->subNumber('MAJ');//提交订单次数统计
            }
        }

    }

    public function jieguoye(){
        $ordernum=$_REQUEST['ordernum'];
        if(empty($ordernum)){
            $ordernum=cookie('orderid');
        }
        $arrdata[0]=S('sm_h5test|'.$ordernum);
        if($arrdata[0]['price']==''){
            $arrdata=M()->query("select * from sm_h5test where ordernum='{$ordernum}'");
        }
        $this->assign('createtime',$arrdata[0]['createtime']);
        $pid=$arrdata[0]['remark'];
        $majiang=new \Library\MaJiang();
        $arr=$majiang->index($pid);
        $this->assign('arr',$arr);
        $this->assign('active',3);
        $this->assign('paid',1);

        $this->assign('kind',0);
        $this->display('Recreation/index');
    }

    public function pay(){//支付宝支付
        $this->zhifu();
        $this->assign('ordername1','每日牌运');
        $this->assign('ordername2','知命-每日牌运');
        $this->display("Index/pay");
    }
    public function paywx(){//H5微信支付
        $this->zhifu();
        H5ZMwxzhifu('MAJ','每日牌运',cookie('orderid'),'Recreation');//知命
    }
    public function paywxGzh(){//微信公众号支付
        $this->zhifu();
        $xml=wxgzhzf('MAJ','每日牌运',cookie('orderid'));//公众号支付回调参数
        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val = json_decode(json_encode($xmlstring),true);
        $noncestr=noncestr(15);//随机字符串
        $timestamp=time();//时间戳

        $stringA="appId=".cookie('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
        $stringSignTemp=$stringA."&key=".cookie('wxKEY');
        $sign=strtoupper(md5($stringSignTemp));


        $arr=array(
            'timeStamp'=>strval($timestamp),
            'nonceStr'=>$noncestr,
            'package'=>'prepay_id='.$val['prepay_id'],
            'signType'=>'MD5',
            'paySign'=>$sign,
            'appId'=>cookie('APPID')
        );
        echo json_encode($arr);
    }
    public function downOrder(){//下单
        $this->zhifu();
        echo json_encode(cookie('orderid'));
    }


}